//
//  ViewController.swift
//  keyBordViewPiker
//
//  Created by Mac on 2019/8/14.
//  Copyright © 2019 Mac. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var mytextField: UITextField!
    var kPKeyBoardView:KPKeyBoardView = KPKeyBoardView.loadFromNib()
    override func viewDidLoad() {
        super.viewDidLoad()
      
       NotificationCenter.default.addObserver(self, selector: #selector(selectNameString(notifi:)), name: NSNotification.Name(rawValue:"SelectBankNameNoti"), object: nil)
        kPKeyBoardView.listView.register(UINib.init(nibName: "BankSelectListCell", bundle: nil), forCellReuseIdentifier: "BankSelectListCellID")
        self.mytextField.inputView = self.kPKeyBoardView
        kPKeyBoardView.selectBankName = { BankName in
            debugPrint("a",BankName)
            self.mytextField.text = BankName
            self.mytextField.resignFirstResponder()
        }
    }
    @objc  func selectNameString(notifi:NSNotification){
     debugPrint(1)
        let dic = notifi.userInfo
    
        self.mytextField.text = dic?["SelectBankNameValue"] as? String
    }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    @objc func keyBoardWillHide(_notification: Notification){
        
    }
    @objc func keyBoardWillShow(_notification: Notification){
        
    }
}


class KeyBoardView: UIView{
    var sourceData:[String] = ["中信银行","建设银行","百度银行"]
    var backSelect:((String)->Void)?
}
extension KeyBoardView:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sourceData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "KeyBoardViewCellID")
        cell?.textLabel?.text = self.sourceData[indexPath.row]
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        debugPrint(self.sourceData[indexPath.row])
        if  self.backSelect != nil {
            self.backSelect!(self.sourceData[indexPath.row])
        }
    }
    
}
